﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using TMT_CMS.BL.Model;
using System.Reflection;
using System.Web.Routing;
using TMC_CMS.Controllers;

namespace TMC_CMS.Utility
{
    public static class HtmlHelperExtension
    {
        /// <summary>
        /// help to build pagging footer 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="helper"></param>
        /// <param name="paggingModel"></param>
        /// <returns></returns>
        public static MvcHtmlString BuildPaggingFooter<T1, T2>(this HtmlHelper helper, PaggingResultModel<T1, T2> paggingModel, string controllerName, string actionName)
        {
            if (paggingModel.TotalPageCount == 0)
            {
                return new MvcHtmlString(string.Empty);
            }

            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"right\">");

            if (paggingModel.CurrentPageIndex != 0)
            {
                string firstUrl = BuildUrl<T2>(urlHelper, controllerName, actionName, 0, paggingModel.SearchCondition);
                sb.AppendFormat("<a href=\"{0}\">第一页</a>", firstUrl);

                string previousUrl = BuildUrl<T2>(urlHelper, controllerName, actionName, paggingModel.CurrentPageIndex - 1, paggingModel.SearchCondition);
                sb.AppendFormat("<a href=\"{0}\">前一页</a>", previousUrl);
            }

            if (paggingModel.CurrentPageIndex > 3)
            {
                sb.AppendLine("<span>...</span>");
            }

            List<int> pageShowingList = GetPageIndexForShowing(paggingModel.TotalPageCount, paggingModel.CurrentPageIndex);

            foreach (int pageIndex in pageShowingList)
            {
                string url = BuildUrl<T2>(urlHelper, controllerName, actionName, pageIndex, paggingModel.SearchCondition);
                string activeClass = string.Empty;
                if (pageIndex == paggingModel.CurrentPageIndex)
                {
                    activeClass = "class=\"active\"";
                    url = "#";
                }
                sb.AppendLine(string.Format("<a href=\"{0}\" {1}>{2}</a>", url, activeClass, pageIndex + 1));
            }

            if (paggingModel.TotalPageCount - pageShowingList.Last() > 3)
            {
                sb.AppendLine("<span>...</span>");
            }

            if (paggingModel.CurrentPageIndex != paggingModel.TotalPageCount - 1)
            {
                string nextUrl = BuildUrl<T2>(urlHelper, controllerName, actionName, paggingModel.CurrentPageIndex + 1, paggingModel.SearchCondition);
                sb.AppendFormat("<a href=\"{0}\">后一页</a>", nextUrl);

                string lastUrl = BuildUrl<T2>(urlHelper, controllerName, actionName, paggingModel.TotalPageCount - 1, paggingModel.SearchCondition);
                sb.AppendFormat("<a href=\"{0}\">最后一页</a>", lastUrl);
            }

            sb.AppendLine("</div>");
            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString BuildViewCount<T1, T2>(this HtmlHelper helper, PaggingResultModel<T1, T2> paggingModel)
        {
            if (paggingModel.TotalPageCount == 0)
            {
                return new MvcHtmlString("<div class=\"left\">显示 0 of 0 </div>");
            }

            StringBuilder sb = new StringBuilder();
            int startIndex = paggingModel.StartItemIndex;
            int endIndex = paggingModel.StartItemIndex + paggingModel.ResultList.Count - 1;

            if (startIndex == endIndex)
            {
                sb.AppendFormat("<div class=\"left\">显示 {0} of {1}</div>", startIndex, paggingModel.TotalItemCount);
            }
            else
            {
                sb.AppendFormat("<div class=\"left\">显示 {0}-{1} of {2}</div>", startIndex, endIndex, paggingModel.TotalItemCount);
            }
            return new MvcHtmlString(sb.ToString());
        }

        private static List<int> GetPageIndexForShowing(int totalPageCount, int currentPageIndex)
        {
            var retList = new List<int>();

            int startIndex = currentPageIndex - 2;
            if (totalPageCount - currentPageIndex - 1 < 2)
            {
                startIndex -= 2 - (totalPageCount - currentPageIndex - 1);
            }

            if (startIndex < 0)
            {
                startIndex = 0;
            }

            while (retList.Count < 5 && startIndex < totalPageCount)
            {
                retList.Add(startIndex);
                startIndex++;
            }

            return retList;
        }

        private static string BuildUrl<T>(UrlHelper url, string controllerName, string actionName, int pageIndex, T searchCondition)
        {
            var routeDic = new RouteValueDictionary();
            routeDic.Add("PageIndex", pageIndex);

            PropertyInfo[] properties = typeof(T).GetProperties();
            if (properties != null && properties.Length != 0)
            {
                foreach (PropertyInfo property in properties)
                {
                    routeDic.Add(property.Name, property.GetValue(searchCondition, null));
                }
            }

            return url.Action(actionName, controllerName, routeDic);
        }

        public static MvcHtmlString BuildNavigationTabs(this HtmlHelper helper)
        {
            var controllerType = ControllerActionNameHelper.GetControllerType(helper.ViewContext.Controller);
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            StringBuilder sb = new StringBuilder();
            sb.Append("<ul>");

            string activeClass = ControllerActionNameHelper.GetActiveClass(controllerType, ControllerActionNameHelper.ControllerType.VENDOR);
            string url = urlHelper.Action("Index", "Vendor");
            sb.AppendFormat("<li><a href=\"{0}\" {1}><span>供应商基本信息</span></a></li>", url, activeClass);

            activeClass = ControllerActionNameHelper.GetActiveClass(controllerType, ControllerActionNameHelper.ControllerType.CONTRACT);
            url = urlHelper.Action("Index", "Contract");
            sb.AppendFormat("<li><a href=\"{0}\" {1}><span>供应商合同信息</span></a></li>", url, activeClass);

            sb.Append("<li><a href=\"attachment.html\"><span>供应商报价信息</span></a></li>");
            sb.Append("<li><a href=\"freightquery.html\"><span>运价查询</span></a></li>");

            activeClass = ControllerActionNameHelper.GetActiveClass(controllerType, ControllerActionNameHelper.ControllerType.BASICDATA);
            url = urlHelper.Action("Index", "BasicData");
            sb.AppendFormat("<li><a href=\"{0}\" {1}><span>基础数据维护</span></a></li>", url, activeClass);

            activeClass = ControllerActionNameHelper.GetActiveClass(controllerType, ControllerActionNameHelper.ControllerType.USER);
            url = urlHelper.Action("Index", "User");
            sb.AppendFormat("<li><a href=\"{0}\" {1}><span>用户权限管理</span></a></li>", url, activeClass);

            sb.Append("</ul>");
            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString BuildBreadCrumb(this HtmlHelper helper)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"small-nav\">");

            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            var controllerType = ControllerActionNameHelper.GetControllerType(helper.ViewContext.Controller);
            string controllerName = ControllerActionNameHelper.GetControllerName(controllerType);
            string homeUrl = urlHelper.Action("Index", ControllerActionNameHelper.GetControllerID(controllerType));

            sb.AppendFormat("<a href=\"{0}\">{1}</a>", homeUrl, controllerName);
            //sb.Append("&nbsp;<span>&gt;</span>&nbsp;");
            //ViewContext.RouteData.GetRequiredString("action")
            //sb.Append("供应商报价信息");
            sb.Append("</div>");

            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString BuildMessagesList(this HtmlHelper helper)
        {
            StringBuilder sb = new StringBuilder();
            if (helper.ViewContext.TempData[Const.TEMPDATA_MESSAGES] != null)
            {
                var messageList = (List<string>)helper.ViewContext.TempData[Const.TEMPDATA_MESSAGES];
                int i = 0;
                foreach (var message in messageList)
                {
                    sb.AppendFormat("<div class=\"msg msg-ok\" id=\"{0}\" ><p><strong>{1}</strong></p><a href=\"#\" class=\"close\" onclick=\"$('#{2}').css('display','none');\">close</a></div>", "message" + i, message, "message" + i);
                    i++;
                }
            }
            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString BuildErrorList(this HtmlHelper helper)
        {
            StringBuilder sb = new StringBuilder();
            if (helper.ViewContext.TempData[Const.TEMPDATA_ERRORS] != null)
            {
                var messageList = (List<string>)helper.ViewContext.TempData[Const.TEMPDATA_ERRORS];
                int i = 0;
                foreach (var message in messageList)
                {
                    sb.AppendFormat("<div class=\"msg msg-error\" id=\"{0}\" ><p><strong>{1}</strong></p><a href=\"#\" class=\"close\" onclick=\"$('#{2}').css('display','none');\">close</a></div>", "error" + i, message, "error" + i);
                    i++;
                }
            }

            return new MvcHtmlString(sb.ToString());
        }
    }
}